<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/11
 * Time: 16:03
 * @copyright: ©2024 永州微聚商网络科技有限公司
 * @link: https://www.weijuyunke.com
 */


namespace app\admin\controller;
use app\admin\model\RoleMenu;
use app\Request;
use app\admin\model\Role as  RoleModel;

class  Role{
    public  function  roleList( Request $request){
        $roleModel = new RoleModel();
        $data = $roleModel->select()->toArray();
        return adminSuccess('角色列表',$data);
    }

    public function rolePage(Request $request){
        $param = $request->param();
        $roleModel = new RoleModel();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where = [];
        if (isset($param['name']) && !empty($param['name'])) {
            $where['name'] = $param['name'];
        }
        $userList = $roleModel ->limit($start, $limit)->where($where)->select()->toArray();
        $count = $roleModel->count();
        return adminSuccess('角色列表',['list'=>$userList,'count'=> $count]);
    }

    /**
     * @param Request $request
         添加角色
     */
    public function add(Request $request){
        $param = $request->param();
        if (empty($param['name'])){
            return adminError('请输入角色名称');
        }
        $data = [
            'name' => $param['name'],
            'status' =>  1,
            'create_time' => time(),
            'type' => $param['type'],
        ];
        $roleModel = new RoleModel();
        $roleModel->insert($data);
        return adminSuccess('添加成功');
    }

    public function  edit(Request $request){
        $param = $request->param();
        if (empty($param['name'])){
            return adminError('请输入角色名称');
        }
        $data = [
            'name' => $param['name'],
            'status' =>  1,
            'type' => $param['type'],
        ];
        $roleModel = new RoleModel();
        $roleModel->where(['id'=>$param['id']])->update($data);
        return adminSuccess('操作成功');
    }

    public function roleMenu(Request $request){
        $param = $request->param();
        if ($param['role_id'] == 1){
            return adminError('超级管理员拥有全部权限');
        }
        $menuModel = new \app\admin\model\Menu();
        $menuList = $menuModel->getMenu(array_column($request->role,'role_id'));;
        $roleMenuModel = new RoleMenu();
        $roleMenuList = $roleMenuModel->where(['role_id' => $param['role_id'],'type' => 1])->select()->toArray();
        $roleMenuListById = [];
        if ($roleMenuList){
            $roleMenuListById = array_column($roleMenuList,'menu_id');
        }
        foreach ($menuList as $menuKey => $menuValue){
            if (in_array($menuValue['menu_id'],$roleMenuListById)){
                $menuList[$menuKey]['checked'] = true;
            }else{
                $menuList[$menuKey]['checked'] = false;
            }
        }
        return adminSuccess('用户权限',$menuList);
    }


    public function roleMenuEdit(Request $request){
        $param = $request->param();
        $roleMenuModel = new RoleMenu();
        $roleMenuModel->where(['role_id' => $param['role_id']])->delete();
        $data = [];
        if ($param['menu_id']){
            foreach ($param['menu_id'] as $key =>  $value){
                $data[$key]['menu_id'] = $value;
                $data[$key]['role_id'] = $param['role_id'];
            }
            $roleMenuModel->insertAll($data);
        }
        return adminSuccess('成功');

    }

    public function rolePower(Request $request){
        $param = $request->param();
        //foreach ()
        $roleMenuModel = new RoleMenu();
        $roleMenuModel->where(['role_id' => $param['role_id']])->delete();
        $data = [];
        foreach ($param['menu'] as $key =>  $value){
            $data[]  = [
                'menu_id' => $value,
                'role_id' => $param['role_id'],
                'create_time' => time(),
            ];
        }
        $roleMenuModel->insertAll($data);
        return adminSuccess('操作成功');
    }

}

